#include "ui_manager.h"
#include "display_dir.h"
extern Display_dir current_dir;
pButton_t * Touch_Btn;
pButton_t * Net_Btn;
int btn_pre_cnt=0,btn_net_cnt=0;

int default_Button_Pressure(struct Button_t *pcur_btn,pInput_data pcur_input) {
	if(pcur_input->INPUT_TYPE == INPUT_TYPE_TOUCH) {
		if(pcur_btn->if_pressure == PRESSURE_TYPR_TOUCH) {
			//位置比对函数
			/*x , y 等坐标比对 */
		}else {
			return 0;
		}	
	}else if(pcur_input->INPUT_TYPE == INPUT_TYPE_NET){
		if(pcur_btn->if_pressure == PRESSURE_TYPR_NET) {
			/* 网络数据比对字符比对 */
			
			
		}else {
			return 0;
		}
	}else {
		return 0;
	}
	return 1;
	
}

int default_Button_Show(struct Button_t *pcur_btn) {
	
	return 1;
}


int Button_Init(pButton_t pcur_btn,int index,\
	int iXLeft,int iYTop,int Width,int Height,int if_pressure,wchar_t * btn_str,\
	int (*Button_Pressure)(struct Button_t *pcur_btn,pInput_data pcur_input),\
	int (*Button_Show)(struct Button_t *pcur_btn)) {
	int n;
	//分配内存
	//pcur_btn = (pButton_t)malloc(sizeof(Button_t));
	//初始化参数
	pcur_btn->index = index;
	pcur_btn->iXLeft = iXLeft;
	pcur_btn->iYTop = iYTop;
	pcur_btn->Width = Width;
	pcur_btn->Height = Height;
	pcur_btn->if_pressure = if_pressure;
	for ( n = 0; n < wcslen(btn_str); n++ )
	{
		pcur_btn->button_str[n] = btn_str[n];			
	}
	//pcur_btn->button_str = btn_str;
	//看有无配设对应的控制函数
	pcur_btn->Button_Pressure = (Button_Pressure == NULL ? default_Button_Pressure : Button_Pressure);
	pcur_btn->Button_Show = (Button_Show == NULL ? default_Button_Show : Button_Show);
	
	return 0;
}



Page_t Main_Page = {
	.name = "主页面",
	.index = 0,
	.show = Main_Show,
	.handle = Main_handle,
};
	
int Main_Show(int index) {
	char buf[1024];
	char **btn_info;
	//读取主页面配置表
	FILE * config_f;
	int i,btn_cnt=0,heigh,width;
	int start_x,start_y;
	int start_x_pos = 0;
	int start_y_pos = 0;
	int row,column;
	btn_info = (char**)malloc(sizeof(char*)*20);
	for (i = 0; i < 20; ++i){//为每列分配4个大小空间 
		btn_info[i] = (char *)malloc(sizeof(char)*40); 
		memset(btn_info[i],0,sizeof(char)*40);
	} 
			
	config_f = fopen("./config","r");
			
	while(fgets(buf,1024,config_f) != NULL) {
		if(buf[0] == '#' || buf[0] == ' ') {
			continue;
		}
		int index=0;
		while(buf[index] != '\n') {
			btn_info[btn_cnt%20][index] = buf[index];
			index++;
		}		
		btn_cnt++;
		memset(buf,0,1024);
	}
	Touch_Btn = (pButton_t *)malloc(sizeof(pButton_t)*btn_cnt);
	for (i = 0; i < btn_cnt; ++i){ 
		Touch_Btn[i] = (pButton_t)malloc(sizeof(Button_t)); 
		memset(Touch_Btn[i],0,sizeof(pButton_t));
	} 
	Net_Btn = (pButton_t *)malloc(sizeof(pButton_t)*btn_cnt);
	for (i = 0; i < btn_cnt; ++i){//为每列分配4个大小空间 
		Net_Btn[i] = (pButton_t)malloc(sizeof(Button_t)); 
		memset(Net_Btn[i],0,sizeof(pButton_t));
	} 
	
			//展现图标
	Btn_Range(&width, &heigh,btn_cnt);
	column = (1024/width);
	row = (600/heigh);
	start_x = (1024- column*width)/(column + 1);
	start_y = (600- row*heigh)/(row + 1);
	printf("width :%d  heigh :%d \n",width,heigh);
	printf("行 %d 列：%d 按钮个数 cnt %d \n",row,column,btn_cnt);
	start_x_pos += start_x;
	start_y_pos += start_y;
	for(i=1;i<=btn_cnt;i++) {
		int index=0,name_len=0;
		int btn_type;
		char *name_info = (char *)malloc(15); 
		memset(name_info,0,15);
		//提取字符串
		while(btn_info[i-1][index] != ' ') {
			name_info[name_len] = btn_info[i-1][index];
			index++;
			name_len++;
		}
		while(btn_info[i-1][index] == ' ') {
			index++;
		}
		wchar_t *WStr;
		name_len++;
		WStr=(wchar_t*)malloc(name_len*sizeof(wchar_t));
		mbstowcs(WStr, name_info, 100);
		if(btn_info[i-1][index] == '1') {
			//为按压类型  
			Button_Init(Touch_Btn[btn_pre_cnt],i-1,\
						start_x_pos, start_y_pos, start_x_pos+width,start_y_pos+heigh,\
						PRESSURE_TYPR_TOUCH, WStr, NULL,NULL);
			current_dir.show_Button(Touch_Btn[btn_pre_cnt]);
			btn_pre_cnt++;
		}else if(btn_info[i-1][index] == '2') {
			//为网络类型
			char *btn_net_ctl = (char *)malloc(15);
			int ctl_len = 0;
			memset(btn_net_ctl,0,15);
			index++;
			while(btn_info[i-1][index] == ' ' || btn_info[i-1][index] == '"') {
				index++;
			}
			while(btn_info[i-1][index] != '"') {
				btn_net_ctl[ctl_len] = btn_info[i-1][index];
				index++;
				ctl_len++;
			}
			Net_Btn[btn_net_cnt]->ctl_net_str = btn_net_ctl;
			Button_Init(Net_Btn[btn_net_cnt],i-1, \
						start_x_pos, start_y_pos, start_x_pos+width,start_y_pos+heigh,\
						PRESSURE_TYPR_NET, WStr, NULL,NULL);
			current_dir.show_Button(Net_Btn[btn_net_cnt]);
			btn_net_cnt++;
		}else {
			printf("错误类型\n");
			continue;
		}
		
		printf("start_x_pos : %d  start_y_pos : %d \n",start_x_pos,start_y_pos);

		start_x_pos += start_x + width;
		start_y_pos += ((i/column>0 && i%column == 0) ? heigh+start_y : 0);
		if(start_x_pos >= 1000) {
			start_x_pos = start_x;
		}
		if(start_y_pos > 550) {
			start_y_pos = start_y;
		}
		free(WStr);
	}	
	
	return 0;		
}

int Main_handle(pInput_data pcur_in_data) {

	printf("recv data \n");
	if(pcur_in_data->INPUT_TYPE == INPUT_TYPE_TOUCH) {

		int index = get_btn_index(pcur_in_data);		
		//判断函数
		if(index != -1) {
			Touch_Btn[index]->state = STATE_TYPR_PRESSURE;			
			current_dir.show_Button(Touch_Btn[index]);
		}
	}else if(pcur_in_data->INPUT_TYPE == INPUT_TYPE_NET) {
		printf("data type : %s \n",pcur_in_data->net_data);
		int n;
		for(n = 0;n<btn_net_cnt;n++) {
			if(strcmp(pcur_in_data->net_data,Net_Btn[n]->ctl_net_str) == 0) {
				Net_Btn[n]->state = STATE_TYPR_PRESSURE;			
				current_dir.show_Button(Net_Btn[n]);
				printf("第%d个控制指令: %s\n",n,Net_Btn[n]->ctl_net_str);
				break;
			}
				
		}
	}
	return 0;
}
	

int get_btn_index(pInput_data pcur_in_data) {

	int x = pcur_in_data->ix;
	int y = pcur_in_data->iy;
	int n=0;
	for(n;n<btn_pre_cnt;n++) {
		if(x>=Touch_Btn[n]->iXLeft && x<=Touch_Btn[n]->Width) {
			if(y>=Touch_Btn[n]->iYTop && y<=Touch_Btn[n]->Height)
				return n;
		}
	}
	return -1;
}
int Btn_Range(int * width,int * heigh,int btn_cnt) {
			
		int max_x = 1024;
		int max_y = 600;
		
		float rate = (float)max_y / max_x;
		rate = 0.6184;
		int one_size = max_x * max_y / btn_cnt;
		*width = sqrt((float)one_size / rate);
		*width -= 55;
			
		*heigh = (int)((*width) * rate);
		*heigh -= 55 - 55*rate;
		
		return 0;
}

